package com.yzt.loginmodule.login;

import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.yzt.zhuju.arouter.RouterAccount;
import com.yzt.zhuju.arouter.RouterMain;
import com.yzt.zhuju.base.BaseActivity;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.utils.RegexUtils;
import com.yzt.loginmodule.R;
import com.yzt.p_base.utils.ToastUtil;

/**
 * author : bob
 * date   : 2022/12/1
 * desc   : 重置密码
 */
@Route(path = RouterAccount.ACTIVITY_RESET_PASSWORD)
public class ResetPasswordActivity extends BaseActivity {

    @Autowired
    String mobile;

    @Autowired
    String zone;

    @Autowired
    String cookie;

    private ImageView ivOpenOrClose;
    private EditText etSetPwd;
    private TextView tvSure;

    @Override
    public int getLayoutId() {
        return R.layout.activity_reset_password;
    }

    @Override
    public void installViews() {

        setFullScreen(true, R.color.transparent);

        ivOpenOrClose = findViewById(R.id.iv_open_or_close);
        etSetPwd = findViewById(R.id.et_set_passwd);
        tvSure = findViewById(R.id.tv_sure);

        tvSure.setOnClickListener(this);
        ivOpenOrClose.setOnClickListener(this);
        findViewById(R.id.tv_help).setOnClickListener(this);
        findViewById(R.id.iv_delete).setOnClickListener(this);
        findViewById(R.id.iv_title_left).setOnClickListener(this);
        setTextWatcher();
        setCommitStatus();
    }

    @Override
    protected void onClick(int clickId) {
        super.onClick(clickId);
        if (clickId == R.id.tv_help) {
            RouterMain.goToWeb(String.format(getString(R.string.login_user_help)));
        } else if (clickId == R.id.iv_title_left) {
            mActivity.finish();
        } else if (clickId == R.id.tv_sure) {
            resetPwd();
        } else if (clickId == R.id.iv_open_or_close) {
            onShowOrCloseClick();
        } else if (clickId == R.id.iv_delete) {
            etSetPwd.setText("");
        }
    }

    private void onShowOrCloseClick() {
        ivOpenOrClose.setSelected(!ivOpenOrClose.isSelected());
        if (ivOpenOrClose.isSelected()) {
            ivOpenOrClose.setImageResource(R.mipmap.login_password_open);
        } else {
            ivOpenOrClose.setImageResource(R.mipmap.login_password_close);
        }
        etSetPwd.setTransformationMethod(ivOpenOrClose.isSelected() ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
        etSetPwd.setSelection(etSetPwd.getText().length());
    }

    private void setTextWatcher() {
        etSetPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                setCommitStatus();
            }
        });
    }

    private void setCommitStatus() {
        String passWord = etSetPwd.getText().toString();
        findViewById(R.id.iv_delete).setVisibility(passWord.length() > 0 ? View.VISIBLE : View.INVISIBLE);
        tvSure.setEnabled(passWord.length() >= 6);
    }

    private void resetPwd() {
        String password = etSetPwd.getText().toString();
        if (!RegexUtils.checkPassword(password)) {
            ToastUtil.showMessage(R.string.set_input_passwd_error, Toast.LENGTH_SHORT, Gravity.CENTER);
            return;
        }

        showMProgress();
        BizLayer.getInstance().getAccountModule().modifyPwd(mobile, cookie, password,
                new AresSubscriber() {
                    @Override
                    protected void onSuccess(Object o) {
                        ToastUtil.showMessage(R.string.str_update_success);
                        mActivity.setResult(RESULT_OK);
                        mActivity.finish();
                    }

                    @Override
                    protected void onFinallyEnd() {
                        super.onFinallyEnd();
                        closeProgress();
                    }
                });
    }
}

